package com.abso.mp3tunes.locker.ui.actions;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;

import com.abso.mp3tunes.locker.ui.ILockerConstants;
import com.abso.mp3tunes.locker.ui.viewers.LockerViewer;

/**
 * Action able to open a specific browser URL.
 */
public abstract class AbstractOpenBrowserAction extends
		AbstractLockerViewerAction {

	/**
	 * Constructs a new action.
	 * 
	 * @param label
	 *            the action's text.
	 * @param imageDescriptor
	 *            the action's image.
	 * @param lockerViewer
	 *            the Locker tree viewer this action is based on.
	 */
	public AbstractOpenBrowserAction(String label,
			ImageDescriptor imageDescriptor, LockerViewer lockerViewer) {
		super(label, imageDescriptor, lockerViewer);
	}

	/**
	 * Returns the URL to open in browser.
	 * 
	 * @return the URL.
	 */
	protected abstract String getURL();

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run() {
		BusyIndicator.showWhile(null, new Runnable() {
			public void run() {
				IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
						.getBrowserSupport();
				try {
					IWebBrowser browser = support.createBrowser(
							IWorkbenchBrowserSupport.AS_EDITOR
									| IWorkbenchBrowserSupport.NAVIGATION_BAR
									| IWorkbenchBrowserSupport.STATUS
									| IWorkbenchBrowserSupport.LOCATION_BAR,

							ILockerConstants.BROWSER_ID, null, null);
					browser.openURL(new URL(getURL()));
				} catch (MalformedURLException e) {
				} catch (PartInitException e) {
				}
			}
		});

	}

}
